﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoungeRepo.Contracts.Core;
using LoungeRepo.Core;

namespace Using_collections
{
    class Program
    {
        static void Main(string[] args)
        {
            using(var repo = new LoungeRepository())
            {
                var manualExtent = new List<Person>
                                       {
                                           new Person {Name = "Peter", City = "New York"},
                                           new Person {Name = "Paul", City = "Boston"},
                                           new Person {Name = "Mary", City = "Moscow"}
                                       };

                Console.WriteLine("Store");
                foreach(Person p in manualExtent)
                    Console.WriteLine("ext: {0}, {1}", p.Id, p.Name);

                // peter and paul know mary
                manualExtent[0].Aquaintances.Add(manualExtent[2]);
                manualExtent[1].Aquaintances.Add(manualExtent[2]);

                // mary knows peter and paul
                manualExtent[2].Aquaintances.Add(manualExtent[0]);
                manualExtent[2].Aquaintances.Add(manualExtent[1]);

                repo.Store(manualExtent, "personExtent");
            }

            using(var repo = new LoungeRepository())
            {
                var manualExtent = repo.Load<List<Person>>("personExtent");

                Console.WriteLine("Load");
                foreach (Person p in manualExtent)
                    Console.WriteLine("ext: {0}, {1}", p.Id, p.Name);

                // this is to be expected:
                // when loading a root all nested objects are loaded as well
                Console.WriteLine("--- listing entities in manual extent");
                foreach(Person p in manualExtent)
                {
                    Console.WriteLine(p);
                    foreach (Person a in p.Aquaintances)
                        Console.WriteLine("  {0}", a);
                }

                // but this is working, too:
                // nested entities are also added to the cache upon retrieval of their root
                Console.WriteLine("--- listing entities in auto extent");
                foreach (Person p in repo.GetExtent<Person>())
                {
                    Console.WriteLine(p);
                    foreach (Person a in p.Aquaintances)
                        Console.WriteLine("  {0}", a);
                }
            }
        }
    }
}
